package com.scoy.honeymei.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.MainActivity;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.LoginBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: LoginActivity$
 * @Description: 绑定手机号界面
 * @Author: GaoZhan
 * @CreateDate: 2019/8/30/0030$ 上午 10:20$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/30/0030$ 上午 10:20$
 * @UpdateRemark: 更新内容
 */
public class AttachActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.lg_sign_tv)
    TextView lgSignTv;
    @BindView(R.id.lg_phone_et)
    EditText lgPhoneEt;
    @BindView(R.id.lg_yzm_et)
    EditText lgYzmEt;
    @BindView(R.id.lg_yzm_tv)
    TextView lgYzmTv;
    @BindView(R.id.lg_yzm_llt)
    LinearLayout lgYzmLlt;
    @BindView(R.id.lg_login_tv)
    TextView lgLoginTv;

    private AttachActivity mContext;
    private String openid;
    private String loginPhone;
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attach);
        ButterKnife.bind(this);
        mContext = AttachActivity.this;
        openid = getIntent().getStringExtra("openid");

        initNormal();
    }

    @Override
    public void initNormal() {
        MyUtil.setStatusBar(mContext, getWindow(), false, 0);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
    }

    @OnClick({R.id.lg_yzm_tv, R.id.lg_login_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lg_yzm_tv:
                if (TextUtils.isEmpty(lgPhoneEt.getText())) {
                    return;
                }
                if (!CheckUtil.isMobileNO(lgPhoneEt.getText().toString())) {
                    MyUtil.mytoast(mContext, "请输入正确的手机号");
                    return;
                }
                httpgetCode();
                break;
            case R.id.lg_login_tv:
                if (MyUtil.isFastClick()) {
                    preLogin();
                }
                break;
        }
    }

    //验证码
    private void httpgetCode() {
        HttpParams params = new HttpParams();
        params.put("phone", lgPhoneEt.getText().toString());
        params.put("type", 2);//1=登录、2=绑定手机号
        HpGo.newInstance().HttpGo(mContext, MyUrl.GET_CODE, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                loginPhone = lgPhoneEt.getText().toString();
                countDownTimer = MyUtil.startTime(lgYzmTv);
                MyUtil.mytoast(mContext, (String) o);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    private void preLogin() {
        if (TextUtils.isEmpty(lgPhoneEt.getText())) {
            MyUtil.mytoast(mContext, "请输入手机号");
            return;
        }
        if (!CheckUtil.isMobileNO(lgPhoneEt.getText().toString())) {
            MyUtil.mytoast(mContext, "请输入正确的手机号");
            return;
        }

        if (TextUtils.isEmpty(lgYzmEt.getText())) {
            MyUtil.mytoast(mContext, "请输入验证码");
            return;
        }
      /*  if (!loginPhone.equals(lgPhoneEt.getText().toString())) {
            MyUtil.mytoast(mContext, "0，手机号或验证码错误");
             return;
        }*/

        httpLogin();
    }

    //登录
    private void httpLogin() {
        HttpParams params = new HttpParams();
        params.put("phone", lgPhoneEt.getText().toString());
        params.put("code", lgYzmEt.getText().toString());
        params.put("openid", openid);
        HpGo.newInstance().HttpGo(mContext, MyUrl.ATTCH_LOGIN, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                LoginBean loginBean = new Gson().fromJson((String) o, LoginBean.class);

                SPHelper.applyString(MeConstant.PHONE, loginBean.getPhone());
                SPHelper.applyString(MeConstant.UID, loginBean.getId()+"");

                Intent intent0 = new Intent(mContext, MainActivity.class);
                startActivity(intent0);
                finish();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}
